package com.bidcms.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * 
 * 保存/读取json文件
 * 
 * */
public class JsonToFile {

	public static void deletecomponentjson(String id,String ServletContextpath) {
		
		File f = new File(ServletContextpath + "/component/" + id
					+ ".json");
		if(f.exists())
			f.delete();
	}

	public static void deletefeaturejson(String id,String ServletContextpath) {
		File f = new File(ServletContextpath + "/feature/" + id
				+ ".json");
		if(f.exists())
			f.delete();
	}

	public static void deletefeature_classjson(String id,String ServletContextpath) {
		File f = new File(ServletContextpath + "/feature_class/" + id
				+ ".json");
		if(f.exists())
			f.delete();
	}

	public static String getcomponentjson(String id, String ServletContextpath) {
		FileReader fr = null;
		BufferedReader br = null;
		StringBuilder sb = new StringBuilder();
		try {
			fr = new FileReader(ServletContextpath + "/component/" + id
					+ ".json");
			br = new BufferedReader(fr);
			String s;
			while ((s = br.readLine()) != null) {
				sb.append(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return sb.toString();
	}

	public static void savecomponent(String id, String json,
			String ServletContextpath) {
		FileWriter fw = null;
		try {
			// column.txt
			fw = new FileWriter(ServletContextpath + "/component/" + id
					+ ".json");

			/*
			 * //Java文件路径问题 System.out.println(
			 * Thread.currentThread().getContextClassLoader().getResource(""));
			 * System
			 * .out.println(VoteDiaochaAction.class.getClassLoader().getResource
			 * ("")); System.out.println(ClassLoader.getSystemResource(""));
			 * System.out.println(VoteDiaochaAction.class.getResource(""));
			 * System.out.println(VoteDiaochaAction.class.getResource("/"));
			 * System.out.println(new File("column2.txt").getAbsolutePath());
			 * System.out.println(System.getProperty("user.dir"));
			 */

			// FileWriter fw=new FileWriter
			// ("/webRoot/amcolumn/data/column.txt");

			// 将字符串写入文件

			fw.write(json.toCharArray());
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getfeaturejson(String id, String ServletContextpath) {
		FileReader fr = null;
		BufferedReader br = null;
		StringBuilder sb = new StringBuilder();
		try {
			fr = new FileReader(ServletContextpath + "/feature/" + id + ".json");
			br = new BufferedReader(fr);
			String s;
			while ((s = br.readLine()) != null) {
				sb.append(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return sb.toString();
	}

	public static void savefeature(String id, String json,
			String ServletContextpath) {
		FileWriter fw = null;
		try {
			// column.txt
			fw = new FileWriter(ServletContextpath + "/feature/" + id + ".json");

			/*
			 * //Java文件路径问题 System.out.println(
			 * Thread.currentThread().getContextClassLoader().getResource(""));
			 * System
			 * .out.println(VoteDiaochaAction.class.getClassLoader().getResource
			 * ("")); System.out.println(ClassLoader.getSystemResource(""));
			 * System.out.println(VoteDiaochaAction.class.getResource(""));
			 * System.out.println(VoteDiaochaAction.class.getResource("/"));
			 * System.out.println(new File("column2.txt").getAbsolutePath());
			 * System.out.println(System.getProperty("user.dir"));
			 */

			// FileWriter fw=new FileWriter
			// ("/webRoot/amcolumn/data/column.txt");

			// 将字符串写入文件

			fw.write(json.toCharArray());
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getfeature_classjson(String id,
			String ServletContextpath) {
		FileReader fr = null;
		BufferedReader br = null;
		StringBuilder sb = new StringBuilder();
		try {
			fr = new FileReader(ServletContextpath + "/feature_class/" + id
					+ ".json");
			br = new BufferedReader(fr);
			String s;
			while ((s = br.readLine()) != null) {
				sb.append(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return sb.toString();
	}

	public static void savefeature_class(String id, String json,
			String ServletContextpath) {
		FileWriter fw = null;
		try {
			// column.txt
			fw = new FileWriter(ServletContextpath + "/feature_class/" + id
					+ ".json");

			/*
			 * //Java文件路径问题 System.out.println(
			 * Thread.currentThread().getContextClassLoader().getResource(""));
			 * System
			 * .out.println(VoteDiaochaAction.class.getClassLoader().getResource
			 * ("")); System.out.println(ClassLoader.getSystemResource(""));
			 * System.out.println(VoteDiaochaAction.class.getResource(""));
			 * System.out.println(VoteDiaochaAction.class.getResource("/"));
			 * System.out.println(new File("column2.txt").getAbsolutePath());
			 * System.out.println(System.getProperty("user.dir"));
			 */

			// FileWriter fw=new FileWriter
			// ("/webRoot/amcolumn/data/column.txt");

			// 将字符串写入文件

			fw.write(json.toCharArray());
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
